package com.party.admin.biz.moneyManagement;

import com.party.common.utils.DateUtils;
import com.party.core.model.YesNoStatus;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.order.OrderType;
import com.party.core.model.wallet.WithdrawalAuditOperate;
import com.party.core.model.wallet.WithdrawalStatus;
import com.party.core.model.wallet.Withdrawals;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.wallet.IWithdrawalAuditOperateService;
import com.party.core.service.wallet.IWithdrawalService;
import com.party.admin.biz.order.OrderBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资金管理
 * Created by hqh
 *
 * @date 2018/11/20 0014
 * @time 16:58
 */

@Service("MoneyManagementBizService")
@Transactional
public class MoneyManagementBizService {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IMemberGroupService memberGroupService;

    @Autowired
    private OrderBizService orderBizService;

    @Autowired
    private IWithdrawalService withdrawalService;

    @Autowired
    private IWithdrawalAuditOperateService withdrawalAuditOperateService;



    /**
     * 判断是否有提现权限
     *
     * @param partnerId 父id
     * @param memberId  用户id，可能是父id，可能是子id
     * @return
     * @author yifeng
     * @date 9:49 2019/9/24
     **/
    public boolean estimateWithdrawalPermissions(String partnerId, String memberId){


        MemberGroup member = memberGroupService.get(partnerId);

        boolean isBand = (null != member) && (YesNoStatus.YES.getCode().equals(member.getOpenStatus())) ;
        double totalAccount = orderBizService.getTotalAccount(memberId,OrderType.ORDER_CROWD_FUND.getCode(),Withdrawals.TYPE_CROWD_FUNDING);
        if (totalAccount > Double.valueOf(0.0d)) {
            isBand = true;
        }
        return isBand;
    }

    /**
     * 判断本周是否有提现成功的记录
     * @return
     */
    public boolean estimateWithdrawalLog(String memberId){
        Withdrawals withdrawals = new Withdrawals();
        withdrawals.setCreateBy(memberId);
        withdrawals.setType(Withdrawals.TYPE_CROWD_FUNDING);
        withdrawals.setStatus(WithdrawalStatus.STATUS_AUDIT_REJECT.getCode());

        Map<String,Object> mmp = new HashMap<>();
        mmp.put("staDate",DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0)));
        mmp.put("endDate",DateUtils.formatDate(DateUtils.getLastDayOfWeek(0)));
        Withdrawals withdrawals1=  withdrawalService.queryWithdrawalsByDate(withdrawals,mmp);

        return withdrawals1!=null;
    }

    /**
     * 判断活动类型并且是否已经结束
     * @return
     */
    public boolean estimateActivityType(CrowdfundEvent event){
        boolean evOF = true;

        Integer eventType =event.getEventType();
        //高端活动
        if(1==eventType){
            // 比较两个日期date1大于date2 返回1 等于返回0 小于返回-1
           int sta= DateUtils.compareDate(event.getEndTime(),new Date());
           return  sta==-1;
        }
        return  evOF;
    }

    /**
     * 判断该主题是否有尾款提现记录和主题是否已经截止
     * @return
     */
    public boolean finalPayment(String eventId,String memberId){
        boolean of = false;
        List<WithdrawalAuditOperate> waolist = withdrawalAuditOperateService.queryTailParagraphInfo(eventId,memberId);
        if(waolist.size()>0){
            of = true;
        }
        return of;
    }
}
